package com.example.administrator.szb.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.administrator.szb.R;

import java.util.List;

/**
 * Created by stone on 2018/1/24.
 */

public class BottomListPop {
    public static void show(View vvv, final Activity context, List<String> list, final OnItemClickListener listener){
        final View popView = LayoutInflater.from(context).inflate(
                R.layout.bottom_pop_list, null);
        ListView listView = popView.findViewById(R.id.listView);
        final PopupWindow popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                popupWindow.dismiss();
                if (listener!=null){
                    listener.onItemClick(position);
                }
            }
        });
        listView.setAdapter(new MyAdapter(context, list));

        setBackgroundAlpha(context,0.5f);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        popupWindow.setOutsideTouchable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(context,1.0f);
            }
        });
        popupWindow.showAtLocation(vvv, Gravity.BOTTOM,0,0);
    }
    public static void setBackgroundAlpha(Activity context,float bgAlpha) {
        WindowManager.LayoutParams lp = (context).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) context).getWindow().setAttributes(lp);
    }
    private void initEvent(){

    }
   static class  MyAdapter extends BaseAdapter {
        private Context contexts;
        private List lists;
        LayoutInflater inflaters;
        public MyAdapter(Context context, List list) {
            this.contexts = context;
            this.lists = list;
            inflaters = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public  Object getItem(int position) {
            return lists.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView==null){
                convertView = inflaters.inflate(R.layout.bottom_pop_item,null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv_name.setText(""+lists.get(position));

            return convertView;
        }

        class ViewHolder{
            TextView tv_name;
            public ViewHolder(View view){
                tv_name  = view.findViewById(R.id.tv_name);
            }
        }
    }
 public static interface OnItemClickListener{
     void onItemClick(int pos);
 }
}
